<?php

class Controller extends CController {

    public $layout = '//layouts/column1';
    public $menu = array();
    public $breadcrumbs = array();
    
    
    
    function getRemainingPackageDay($id){
        //$mts = MemberTransactionHistory::model()->find("member_id=:member_id and isDeposit='1' order by member_transaction_history_id desc limit 1", array('member_id' => $id));
        $sql = "select * from member_transaction_history where member_id =" . $id . " and isDeposit='1' order by member_transaction_history_id desc limit 1";

        $_lh = Yii::app()->db->createCommand($sql)->queryAll();
        $tdate = 0;
        if (!empty($_lh)) {
            $_history = $_lh[0];
            if (!empty($_history)) {
                $tdate = $_history['transaction_date'];
            }
        }
        
        // get current date and time
        $cdate = date('Y-m-d H:i:s');
        
        $startTimeStamp = strtotime($tdate);
        $endTimeStamp = strtotime($cdate);

        $timeDiff = abs($endTimeStamp - $startTimeStamp);
        $numberDays = $timeDiff / 86400;  // 86400 seconds in one day
        $numberDays = intval($numberDays);
        $remainingday = 180 - $numberDays;
        
        return $remainingday; 
    }   
    
//    function getCurrentBalance($id) {
//        $sql = "select * from member_transaction_history where member_transaction_history_id = (
//                    select max( member_transaction_history_id ) from member_transaction_history 
//                    where member_id =" . $id . ")";
//
//        $_lh = Yii::app()->db->createCommand($sql)->queryAll();
//        $balance = 0;
//        if (!empty($_lh)) {
//            $_history = $_lh[0];
//            if (!empty($_history)) {
//                $balance = $_history['balance'];
//            }
//        }
//        if (empty($balance)) {
//            $balance = 0;
//        }
//        return $balance;
//    }
    function getCurrentBalance($id) {
        $credit = 0;
        $debit = 0;

        $sql = "select sum(credit) credit, sum(debit) debit from member_transaction_history where member_id = $id and (isDeposit is NULL or isDeposit <> '1')";
        $_lh = Yii::app()->db->createCommand($sql)->queryAll();
        if (!empty($_lh)) {
            $_history = $_lh[0];
            if (!empty($_history)) {
                $credit = $_history['credit'];
                $debit = $_history['debit'];
            }
        }
        return $credit - $debit;
    }

    function getCurrentUserBalance($id) {
        $credit = 0;
        $debit = 0;

        $sql = "select sum(credit) credit, sum(debit) debit from member_transaction_history where member_id = $id and (isDeposit is NULL or isDeposit <> '1')";
        $_lh = Yii::app()->db->createCommand($sql)->queryAll();
        if (!empty($_lh)) {
            $_history = $_lh[0];
            if (!empty($_history)) {
                $credit = $_history['credit'];
                $debit = $_history['debit'];
            }
        }
        return $credit - $debit;
    }
    
    function getMemberTransaction($type, $member_id, $isRef) {
        $method = "credit";
        if ($type == "D") {
            $method = "debit";
        }
        $sql = "SELECT sum($method) sum FROM member_transaction_history where member_id = $member_id    
			 and payment_processors_id is not null";

        if ($isRef == TRUE) {
            $sql = $sql . " and ref_id is not null";
        }else if ($isRef == FALSE){
            $sql = $sql . " and ref_id is null";
        }
        $command = Yii::app()->db->createCommand($sql);
        $sum = $command->queryScalar();
        if (empty($sum)) {
            $sum = 0;
        }
        return $sum;
    }
    
    function getMemberDeposit($member_id) {
        $sql = "SELECT sum(debit) sum FROM member_transaction_history where member_id = $member_id    
			 and isDeposit='1'";

        $command = Yii::app()->db->createCommand($sql);
        $sum = $command->queryScalar();
        if (empty($sum)) {
            $sum = 0;
        }
        return $sum;
    }
    
    function getTotalEarnings($type, $member_id) {
        $method = "credit";
        if ($type == "D") {
            $method = "debit";
        }
        $sql = "SELECT sum($method) sum FROM member_transaction_history where member_id = $member_id    
			 and payment_processors_id is null";

        $command = Yii::app()->db->createCommand($sql);
        $sum = $command->queryScalar();
        if (empty($sum)) {
            $sum = 0;
        }
        return $sum;
    }
    
    function getTotalReferralEarnings($type, $member_id) {
        $method = "credit";
        if ($type == "D") {
            $method = "debit";
        }
        $sql = "SELECT sum($method) sum FROM member_transaction_history where member_id = $member_id    
			 and payment_processors_id is null and ref_id is not null and resources_id is null";

        $command = Yii::app()->db->createCommand($sql);
        $sum = $command->queryScalar();
        if (empty($sum)) {
            $sum = 0;
        }
        return $sum;
    }
    
    function getTotalGenerationEarnings($type, $member_id) {
        $method = "credit";
        if ($type == "D") {
            $method = "debit";
        }
        $sql = "SELECT sum($method) sum FROM member_transaction_history where member_id = $member_id    
			 and payment_processors_id is null and ref_id is null and resources_id is not null";

        $command = Yii::app()->db->createCommand($sql);
        $sum = $command->queryScalar();
        if (empty($sum)) {
            $sum = 0;
        }
        return $sum;
    }
}